<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-08-08 20:06
 *
 * Version: 2.0
 */

namespace App\Modules\Order\Transformers;


use League\Fractal\TransformerAbstract;

class OrderListTransformer extends TransformerAbstract
{
	private $list;
	private $isBarber;
	public function __construct($orderList,$isBarber=true)
	{
		$this->list = $orderList;
		$this->isBarber = $isBarber;
	}

	public function transform()
	{
		foreach ($this->list->items() as $v) {
			empty($v->user) ?: empty($v->user->picture) ?: $v->user->portrait = $v->user->picture->image;
			if (isset($v->user->picture)) {
				unset($v->user->picture);
			}

			if(!empty($v->comment->resource)){
				foreach ($v->comment->resource as $ks){
					$ks->url;
				}
			}


			if (!empty($v->items)) {
				foreach ($v->items as $g) {
					$g->service;
				}
			};

			empty($v->assistant) ?: empty($v->assistant->picture) ?: $v->assistant->portrait = $v->assistant->picture->image;
			if (isset($v->assistant->picture)) {
				unset($v->assistant->picture);
			}

			if (isset($v->comment)) {
				$v->comment->reply;
				empty($v->comment->user) ? : $v->comment->user->picture;
			}
			$v->appointment;
            $v->cancel;
		}
		$list = $this->list->toArray();
		foreach ($list['data'] as &$v) {
			$v['cancel'] = empty($v['cancel']) ? null : end($v['cancel']);
			isset($v['appointment']['appointment_time']) ? $v['appointment']['appointment_time'] = date('Y-m-d H:i:s', $v['appointment']['appointment_time']) : $v['appointment'];

			if (isset($v['appointment'])) {
				$v['appointment']['nick'] = empty($v['appointment']['nick']) ? $v['appointment']['mobile'] : $v['appointment']['nick'];
			}

			if ($this->isBarber && isset($v['comment']['member'])) {
				$v['comment']['member'] = [
					'name' => $v['comment']['member']['name'],
					'face' => $v['comment']['member']['face']
				];
			}

			if(!$this->isBarber && isset($v['comment']['user'])){
				$v['comment']['user'] = [
					'username' => $v['comment']['user']['username'],
					'portrait' => empty($v['comment']['user']['picture']['image']) ? null : $v['comment']['user']['picture']['image']
				];
			}

			if (isset($v['comment']['resource'])) {
				foreach ($v['comment']['resource'] as $ks) {
					$ks['type'] == config('constant.resourceType.video') ? $v['comment']['video']  =  [
						'url' => $ks['url']['url'],
						'thumb'=> $ks['url']['thumb']
					] : $v['comment']['images'][] = ['image' => isset($ks['url']['image']) ? $ks['url']['image'] : null];
				}
				unset($v['comment']['resource']);
			}

			if (isset($v['comment']['reply'])) {
				$v['comment']['reply'] = [
					'id'         => $v['comment']['reply']['id'],
					'contents'   => $v['comment']['reply']['contents'],
					'created_at' => $v['comment']['reply']['created_at']
				];
			}
			if($v['status'] == config('constant.orderStatus.done')){
				$v['royalty'] = [
					'barber' => isset($v['royalty'][0]['amount']) ? $v['royalty'][0]['amount'] : 0,
					'assistant' => isset($v['royalty'][1]['amount']) ? $v['royalty'][1]['amount'] : 0,

				];
			}
		}
		unset($v);
		return $list;
	}

}
